from django.shortcuts import render
from django.http import JsonResponse, HttpResponse
import os

def index(request):
    return render(request, 'index.html')

def ajax_api(request):
    if request.method == "GET":
        user = {'user1': 'xiaofeng','user2': 'xiaoniu', 'user3': 'xiaoli'}
        code = 0 # 0表示数据处理成功，1表示数据处理失败
        msg = "处理成功！"
        data = {'code': code, 'msg': msg, 'data': user}
        return JsonResponse(data)
    elif request.method == "POST":
        code = 0
        msg = "处理成功！"
        data = {'code': code, 'msg': msg}
        print(request.POST)
        test = request.POST.get("test")
        # 这里就可以根据前端传递过来的数据进行处理，例如操作数据库
        return JsonResponse(data)


def server_api(request):
    if request.method == "GET":
        return render(request, 'server.html')
    elif request.method == "POST":
        code = 0
        msg = "新增服务器成功！"
        data = {'code': code, 'msg': msg}
        print(request.POST)
        hostname = request.POST.get("hostname")
        # 这里就可以根据前端传递过来的数据进行处理，例如操作数据库
        return JsonResponse(data)

def server_table(request):
    if request.method == "GET":
        return render(request, 'server_table.html')
    elif request.method == "POST":
        code = 0
        msg = "删除数据成功！"
        data = {'code': code, 'msg': msg}
        print(request.POST)
        ids = request.POST.getlist("ids[]")
        print(ids)
        # 这里就可以根据操作前端传递过来的数据进行处理，例如操作数据库
        return JsonResponse(data)